/**
 * Sick tabs 
 * 		- Tab style containers written in Mootools
 * 
 * @version			1.12
 * @uses 			Mootools 1.2.3
 * 
 * @param 			{Object} elem
 * @param 			{Object} options
 * 
 * @license			MIT License
 * @author 			hedphug@gmail.com
 * 					barkayal@gmail.com
 */
var sickTabs_t = new Class({
	Implements: [Events, Options],
	
	options : {
		defaultIndex : 0,
		tabHeader : 'tab-triggers', 		// #id
		tabSelector : 'a', 					// element
		tabWrapper : 'tab-wrapper', 		// #id
		tabContainers : '.tab-container', 	// .class
		
		triggersEvent : null, 				// eventHandler - mouseover || click
		setClicked : 'active',
		doAnimation : { },
		cache : true,

		onSelect : function(trigger, container, index) {
			container.addClass('active');
			trigger.addClass(this.options.setClicked);
			if (this.options.defaultIndex != index) window.location.hash = this.getHash(trigger.href);
		},
		onDeselect : function(trigger, container, isSelected) {
			isSelected.removeClass('active');
			trigger.removeClass(this.options.setClicked);
		}
	},
	
	initialize : function(elem, options) {
		__self = this;
		this.setOptions(options);
		this.element = $(elem);
		this.tElem = $(this.options.tabWrapper).getElements(this.options.tabContainers);
		
		var triggers = this.element.getElements(this.options.tabSelector);

		isActive = this.tElem[options.defaultIndex];
		isIndex = triggers[options.defaultIndex];

		this.tElem.each(function(key, index) {
			if (__self.getHash(triggers[index].href) == __self.getHash(window.location.hash)) {
				isActive = key;
				isIndex = triggers[index];
			}
		})

		this.tabFocus(triggers);
	},
	
	tabFocus : function(elems) {
		__self = this;
		__self.fireEvent('select', [isIndex, isActive, __self.options.defaultIndex]);
		
		var evt = this.options.triggersEvent;
		elems.each(function(el, index) {
			el.addEvent(evt, function(et) {
				et.stop();
				
				__self.fireEvent('deselect', [isIndex, __self.tElem[index], isActive]);
				__self.fireEvent('select', [this, __self.tElem[index], index]);
				
				isIndex = elems[index];
				isActive = __self.tElem[index];
			})
		})
	}.protect(),
	
	getHash: function(cur_hash) {
		if (!cur_hash) return false;
		if (cur_hash.contains('#'))
			pos_start_cut = cur_hash.indexOf('#') + 1;
		else
			pos_start_cut = 0;
		return cur_hash.substring(pos_start_cut);
	}.protect()
});



window.addEvent('domready', function() {


	var nSick = new sickTabs_t('tab-triggers', {
		tabSelector : 'a',
		tabWrappers : 'tab-wrapper',
		triggersEvent : 'click',
		defaultIndex : 0,
	});


// EO-F	
})
